import { Avatar, FormRule, PrimaryTableCol, Space, Tag } from 'tdesign-vue-next';

import { GenderEnum } from '@/constants';
import { t } from '@/locales';
import { formatTime } from '@/utils/format';

/**
 * 搜索表单项
 */
export const getSearchParams = () => [
  {
    label: t('pages.userList.username'),
    key: 'username',
    hide: false,
  },
  {
    label: t('pages.userList.nickname'),
    key: 'nickname',
    hide: false,
  },
  {
    label: t('pages.userList.phone'),
    key: 'phone',
    hide: false,
  },
  {
    label: t('pages.userList.email'),
    key: 'email',
    hide: false,
  },
];

/**
 * 表格列
 */
export const getColumns = (): PrimaryTableCol[] => [
  {
    title: t('pages.userList.username'),
    ellipsis: true,
    align: 'center',
    colKey: 'username',
  },
  {
    title: t('pages.userList.avatar'),
    ellipsis: true,
    align: 'center',
    colKey: 'avatar',
    cell: (h, { row }) => h(Avatar, { image: row.avatar }),
  },
  {
    title: t('pages.userList.sex'),
    ellipsis: true,
    align: 'center',
    colKey: 'sex',
    cell: (h, { row }) =>
      h(
        Tag,
        {
          theme: row.sex === 1 ? 'primary' : 'success',
          variant: 'light',
        },
        () => GenderEnum[row.sex],
      ),
  },
  {
    title: t('pages.userList.nickname'),
    ellipsis: true,
    align: 'center',
    colKey: 'nickname',
  },
  {
    title: t('pages.userList.phone'),
    ellipsis: true,
    align: 'center',
    colKey: 'phone',
  },
  {
    title: t('pages.userList.email'),
    ellipsis: true,
    align: 'center',
    colKey: 'email',
  },
  {
    title: '最后登录时间',
    ellipsis: true,
    align: 'center',
    colKey: 'lastLoginTime',
    cell: (h, { row }) => formatTime(row.lastLoginTime),
  },
  {
    title: t('pages.userList.status'),
    ellipsis: true,
    align: 'center',
    colKey: 'status',
    cell: (h, { row }) =>
      h(
        Tag,
        {
          theme: row.status === 1 ? 'success' : 'warning',
          variant: 'light',
        },
        () => (row.status === 1 ? '开启' : '关闭'),
      ),
  },
  {
    title: t('pages.userList.role'),
    ellipsis: true,
    align: 'center',
    colKey: 'roles',
    cell: (h, { row }) =>
      h(
        Space,
        {
          direction: 'vertical',
          size: 'small',
        },
        () => [
          row.roles.map((item: { value: string; label: string }) =>
            h(
              Tag,
              {
                key: item.value,
                size: 'small',
                theme: 'success',
                variant: 'light-outline',
              },
              () => item.label,
            ),
          ),
        ],
      ),
  },
  {
    title: t('pages.userList.remark'),
    ellipsis: true,
    align: 'center',
    colKey: 'remark',
  },
];

/**
 * 表单校验规则
 */
export const getRules = (): Record<string, FormRule[]> => ({
  username: [{ required: true, message: t('pages.userList.required.username'), type: 'error', trigger: 'blur' }],
});
